// 语音播报状态管理
import { create } from 'zustand'

// 定义音频分析数据接口
export interface AudioAnalysisData {
  volume: number;        // 音量 (0-1)
  frequency: number;     // 主要频率 (Hz)
  amplitude: number;     // 振幅 (0-1)
  hasVoiceActivity: boolean; // 是否有声音活动
  timestamp: number;     // 时间戳
}

interface SpeechState {
  isSpeaking: boolean
  isPlaying: boolean
  currentText: string
  volume: number
  speed: number
  pitch: number
  audioData: AudioAnalysisData | null
  mouthAnimationEnabled: boolean
  mouthMorphValue: number  // mouth形态键值 (0或1)
  characterAction: string  // 数字人动作: idle|run|happy|sad
  setSpeaking: (speaking: boolean) => void
  setPlaying: (playing: boolean) => void
  setCurrentText: (text: string) => void
  setVolume: (volume: number) => void
  setSpeed: (speed: number) => void
  setPitch: (pitch: number) => void
  setAudioData: (data: AudioAnalysisData | null) => void
  setMouthAnimationEnabled: (enabled: boolean) => void
  setMouthMorphValue: (value: number) => void
  setCharacterAction: (action: string) => void
  reset: () => void
}

export const useSpeechStore = create<SpeechState>((set) => ({
  isSpeaking: false,
  isPlaying: false,
  currentText: '',
  volume: 1,
  speed: 1,
  pitch: 1,
  audioData: null,
  mouthAnimationEnabled: true,
  mouthMorphValue: 0,  // 默认为0（不播报）
  characterAction: 'idle',
  setSpeaking: (speaking) => set({ isSpeaking: speaking }),
  setPlaying: (playing) => set({ isPlaying: playing }),
  setCurrentText: (text) => set({ currentText: text }),
  setVolume: (volume) => set({ volume }),
  setSpeed: (speed) => set({ speed }),
  setPitch: (pitch) => set({ pitch }),
  setAudioData: (data) => set({ audioData: data }),
  setMouthAnimationEnabled: (enabled) => set({ mouthAnimationEnabled: enabled }),
  setMouthMorphValue: (value) => set({ mouthMorphValue: value }),
  setCharacterAction: (action) => set({ characterAction: action }),
  reset: () => set({
    isSpeaking: false,
    isPlaying: false,
    currentText: '',
    volume: 1,
    speed: 1,
    pitch: 1,
    audioData: null,
    mouthMorphValue: 0,  // 重置时也设为0
    characterAction: 'idle'
  })
}))